<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
require_once (APPPATH.'libraries/userobject.php');
require_once (APPPATH.'libraries/groupobject.php');

/**
 * User Model
 */
class User_Model extends CI_Model {
	
	function __construct()
	{
		parent::__construct();
	}
	
	public function getUserById($id)
	{
		$user = new UserObject();
		
		$query = "SELECT u.id, u.username, u.password, g.id as groupid, g.name as groupname, g.admin as grouptype FROM ".USERS_TABLE. " u, ".GROUPS_TABLE." g WHERE u.group_id = g.id AND u.id=?";
		$data = $this->db->query($query, array($id));
		
		if ($data->num_rows() > 0)
		{
			$row = $data->row();
			$user->setId($row->id);
			$user->setUsername($row->username);
			$user->setPassword($row->password);
			$user->setGroup(new GroupObject($row->groupid, $row->groupname, $row->grouptype));
		}
		
		return $user;
	}
	
	public function getUserByUsername($username)
	{
		$user = new UserObject();
		
		$query = "SELECT u.id, u.username, u.password, g.id as groupid, g.name as groupname, g.admin as grouptype FROM ".USERS_TABLE. " u, ".GROUPS_TABLE." g WHERE u.group_id = g.id AND u.username LIKE N'%".$this->db->escape_like_str($username)."%'";
		$data = $this->db->query($query);
		
		if ($data->num_rows() > 0)
		{
			$row = $data->row();
			$user->setId($row->id);
			$user->setUsername($row->username);
			$user->setPassword($row->password);
			$user->setGroup(new GroupObject($row->groupid, $row->groupname, $row->grouptype));
		}
		
		return $user;
	}
	
	public function insertUser($user)
	{
		$query = "INSERT INTO ".USERS_TABLE.' VALUES (NULL, ?, ?, ?)';
		
		$this->db->trans_start();
		$this->db->query($query, array($user->getUsername(), $user->getPassword(), $user->getGroup()->getId()));
		$this->db->trans_complete();
		
		if ($this->trans_status() === FALSE)
		{
			return INSERT_ERROR;
		}
		
		return ERROR_NONE;
	}
	
	public function updateUser($id, $user)
	{
		$query = 'UPDATE '.USERS_TABLE.' SET password=?, group_id=? WHERE id=?';
		
		$this->db->trans_start();
		$this->db->query($query, array($user->getPassword(), $user->getGroup()->getId(), $id));
		$this->db->trans_complete();
		
		if ($this->db->trans_status() === FALSE)
		{
			return UPDATE_ERROR;
		}
		
		return ERROR_NONE;
	}
	
	public function deleteUser($id)
	{
		$this->db->trans_start();
		$this->db->where('id', $id);
		$this->db->delete(USERS_TABLE);
		$this->db->trans_complete();
		
		if ($this->db->trans_status() === FALSE)
		{
			return UPDATE_ERROR;
		}
		
		return ERROR_NONE;
	}
	
	public function isUserExisted_id($id)
	{
		$this->db->where('id', $id);
		$data = $this->db->get(USERS_TABLE);
		
		return ($data->num_rows() > 0);
	}
	
	public function isUserExisted_username($username)
	{
		$query = "SELECT id FROM " . USERS_TABLE . " WHERE username LIKE N'%" . $this->db->escape_like_str($username) . "%'";
		$data = $this->db->query($query);

		return ($data->num_rows() > 0);
	}
}


?>